package com.ruoyi.project.yingyu.withdraw.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MoneyUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.yingyu.withdraw.domain.UserWithdraw;
import com.ruoyi.project.yingyu.withdraw.mapper.UserWithdrawMapper;
import com.ruoyi.project.yingyu.withdraw.service.IUserWithdrawService;
import com.ruoyi.project.yingyu.withdraw.service.WeixinService;

/**
 * 提现管理Service业务层处理
 * 
 * @author shixiangwen
 * @date 2020-02-14
 */
@Service
public class UserWithdrawServiceImpl implements IUserWithdrawService {

	public static final Logger log = LoggerFactory.getLogger(UserWithdrawServiceImpl.class);

	@Autowired
	private UserWithdrawMapper userWithdrawMapper;

	@Autowired
	WeixinService weixinService;

	/**
	 * 查询提现管理
	 * 
	 * @param id 提现管理ID
	 * @return 提现管理
	 */
	@Override
	public UserWithdraw selectUserWithdrawById(Long id) {
		return userWithdrawMapper.selectUserWithdrawById(id);
	}

	/**
	 * 查询提现管理列表
	 * 
	 * @param userWithdraw 提现管理
	 * @return 提现管理
	 */
	@Override
	public List<UserWithdraw> selectUserWithdrawList(UserWithdraw userWithdraw) {
		List<UserWithdraw> list = userWithdrawMapper.selectUserWithdrawList(userWithdraw);
		for (UserWithdraw model : list) {
			model.setYuan(MoneyUtil.fee2yuan(model.getFee()));
		}
		return list;
	}

	/**
	 * 新增提现管理
	 * 
	 * @param userWithdraw 提现管理
	 * @return 结果
	 */
	@Override
	public int insertUserWithdraw(UserWithdraw userWithdraw) {
		userWithdraw.setCreateTime(DateUtils.getNowDate());
		return userWithdrawMapper.insertUserWithdraw(userWithdraw);
	}

	/**
	 * 修改提现管理
	 * 
	 * @param userWithdraw 提现管理
	 * @return 结果
	 */
	@Override
	public int updateUserWithdraw(UserWithdraw userWithdraw) {
		userWithdraw.setUpdateTime(DateUtils.getNowDate());
		return userWithdrawMapper.updateUserWithdraw(userWithdraw);
	}

	/**
	 * 删除提现管理对象
	 * 
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteUserWithdrawByIds(String ids) {
		return userWithdrawMapper.deleteUserWithdrawByIds(Convert.toStrArray(ids));
	}

	/**
	 * 删除提现管理信息
	 * 
	 * @param id 提现管理ID
	 * @return 结果
	 */
	@Override
	public int deleteUserWithdrawById(Long id) {
		return userWithdrawMapper.deleteUserWithdrawById(id);
	}

	@Override
	public int approve(Long id, Integer status) {
		UserWithdraw userWithdraw = userWithdrawMapper.selectUserWithdrawById(id);
		if (userWithdraw == null || userWithdraw.getApprove() != 0 || userWithdraw.getStatus() != 1) {
			return 0;
		}
		userWithdraw.setApprove(Long.valueOf(status));
		userWithdraw.setApproveBy(ShiroUtils.getLoginName());
		userWithdraw.setApproveTime(DateUtils.getNowDate());
		if (status == 1) {
			userWithdraw.setStatus(2L);
		} else {
			userWithdraw.setStatus(3L);
		}
		userWithdrawMapper.updateUserWithdraw(userWithdraw);

		return 1;
	}

	@Override
	@Transactional
	public int transfer(Long id) {

		UserWithdraw userWithdraw = userWithdrawMapper.selectUserWithdrawById(id);
		if (userWithdraw == null || userWithdraw.getApprove() != 1 || userWithdraw.getTransfer() != 0) {
			return 0;
		}

		int result = 0;

		try {
			boolean success = weixinService.transfer(userWithdraw.getKkid(), userWithdraw.getOpenid(), userWithdraw.getFee().intValue(), userWithdraw.getIp(), userWithdraw.getNickname());
			if (success) {
				userWithdraw.setTransfer(1L);
				result = 1;
			} else {
				userWithdraw.setTransfer(-1L);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			userWithdraw.setTransfer(-1L);
		}

		userWithdraw.setStatus(3L);

		userWithdraw.setTransferBy(ShiroUtils.getLoginName());
		userWithdraw.setTransferTime(DateUtils.getNowDate());
		userWithdrawMapper.updateUserWithdraw(userWithdraw);

		return result;
	}

}
